package com.arron.iteration;

/**
 * @Classname TestStep2
 * @Description TODO
 * @Date 2020/8/15 19:24
 * @Created by 刘
 */
public class TestStep2 {

    /**
     * 有n步台阶，一次只能上1步或者2步，
     *  共有多少种走法
     */
    //使用迭代,性能比递归更加优越
    public static int loop(int n){

        if (n==1 || n==2){
            return n;
        }
        int one = 1;
        int two = 2;
        int sum =one+two;
        for (int i = 3; i <= n; i++) {
            sum = one+two;
            one=two;
            two=sum;
        }
        return sum;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        System.out.println(loop(4));
        System.out.println("耗时："+(System.currentTimeMillis()-start));
    }



}
